/*
	scripts.js
	
	License: GNU General Public License v3.0
	License URI: http://www.gnu.org/licenses/gpl-3.0.html
	
	Copyright: (c) 2013 Alexander "Alx" Agnarson, http://alxmedia.se
*/
(function($){
	//Initialize carousels //////////////////////////////
	function createCarousel(obj) {
		obj.each(function() {
			var $this = $(this);
			$this.carouFredSel({
				responsive : true,
				//prev : '#' + $this.data('previd'),
				//next : '#' + $this.data('nextid'),
				circular : true,
				infinite : true,
				swipe: true,
				scroll : {
					items : 1,
					fx : $this.data('effect'),
					duration : parseInt($this.data('speed'))
				},
				auto : {
					play : !!('off' != $this.data('autoplay')),
					timeoutDuration : 'off' != $this.data('autoplay') ? parseInt($this.data('autoplay')) : 0,
					button: '#breaking-news-play_pause'
				},
				items : {
					visible : {
						min : 1,
						max : 1
					},
					width : 222
				},
				onCreate : function() {
					$this.closest('.slideshow-wrap').css({
						'visibility' : 'visible',
						'height' : 'auto'
					});
					$('.breaking-news .carousel-nav-wrap').remove();
					$('.breaking-news-loader').remove();
					$(window).resize();
				}
			});
		});
	}
	
	var textObject = {	
			delay : 300,
			effect : 'replace',
			classColour : 'blue',
			flash : function(obj, effect, delay) {
				if (obj.length > 0) {
					if (obj.length > 1) {
						jQuery.each(obj, function() {
							effect = effect || textObject.effect;
							delay = delay || textObject.delay;
							textObject.flashExe($(this), effect, delay);					
						});
					} else {
						effect = effect || textObject.effect;
						delay = delay || textObject.delay;
						textObject.flashExe(obj, effect, delay);
					}
				}
			},
			flashExe : function(obj, effect, delay) {
				var flash = setTimeout(function() {
					switch(effect) {
						case 'replace':
						obj.toggle();
						break;
						case 'colour':
						obj.toggleClass(textObject.classColour);
						break;
					}
					textObject.flashExe(obj, effect, delay);
				}, delay);
			}
		};
	
	//3D text plugin
	// jQuery plugin definition
	$.fn.text3d = function(opts) {
	
		// default configuration
		var config = $.extend({}, {
			depth: 5,
			angle: 100,
			color: "#ccc",
			lighten: -0.15,
			shadowDepth: 10,
			shadowAngle: 80,
			shadowOpacity: 0.3
		}, opts);
		
		// to radians
		config.angle = config.angle * Math.PI / 180;
		config.shadowAngle = config.shadowAngle * Math.PI / 180;
		
		// create text shadow
		function TextShadow(e) {
		
			var s = "", i, f, x, y, c;
			
			// 3D effect
			for (i = 1; i <= config.depth; i++) {
				x = Math.round(Math.cos(config.angle) * i);
				y = Math.round(Math.sin(config.angle) * i);
				c = ColorLuminance(config.color, (i-1)/(config.depth-1)*config.lighten);
				s += x+"px "+y+"px 0 "+c+",";
			}
			
			// shadow
			for (f = 1, i = 1; f <= config.shadowDepth; i++) {
				f = f+i;
				x = Math.round(Math.cos(config.shadowAngle) * f);
				y = Math.round(Math.sin(config.shadowAngle) * f);
				c = config.shadowOpacity - ((config.shadowOpacity - 0.1) * i/config.shadowDepth);
				s += x+"px "+y+"px "+f+"px rgba(0,0,0,"+c+"),";
			}
			
			e.style.textShadow = s.substr(0, s.length-1);		
		}
		
		
		// return lighter (+lum) or darker (-lum) color
		function ColorLuminance(hex, lum) {

			// validate hex string
			hex = String(hex).replace(/[^0-9a-f]/gi, '');
			if (hex.length < 6) {
				hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
			}
			lum = lum || 0;
			
			// convert to decimal and change luminosity
			var rgb = "#", c, i;
			for (i = 0; i < 3; i++) {
				c = parseInt(hex.substr(i*2,2), 16);
				c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
				rgb += ("00"+c).substr(c.length);
			}

			return rgb;
		}

		
		// initialization
		this.each(function() {
			TextShadow(this);
		});

		return this;
	};
	
	jQuery(".flash-colour").text3d();
	textObject.flash($('.flash-colour'), 'colour', 400);
	    //end text

	function initGoogleMap() {
		var address = new google.maps.LatLng(21.01269, 105.81984);
		
		var myOptions = {
			zoom:17,
			center: address,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		var image = rootUrl +'img/logo_48x22.jpg';
		
		var marker = new google.maps.Marker({
			position: address, 
			map: map, 
			title: "Văn phòng công chứng Âu Lạc",
			icon : image
		});
		
		var infowindow = new google.maps.InfoWindow({ 
			content:'<div style="color:#0093dd; width: 300px;"><span style="font-weight:bold;font-size:16px;">Văn phòng công chứng Âu Lạc</span><br />Địa chỉ: Số 5 ngõ 121, Thái Hà, Đống Đa, Hà Nội<br />Điện thoại: 0435379018  - Fax : 0435379019<br />Hòm thư: vpcc.aulac@gmail.com</div>',
			size: new google.maps.Size(100,50),
			position: address
		});
		
		infowindow.open(map);
		
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		});
	}
	
	//WINDOW LOAD
	$(window).load(function() {
	
		/////////////////////////////////////////////
		// Carousel initialization
		/////////////////////////////////////////////
		if( typeof $.fn.carouFredSel !== 'undefined' ) {
			createCarousel($('.slideshow'));
		}
	
		/////////////////////////////////////////////
		// Pause breaking news
		/////////////////////////////////////////////
		$('#breaking-news-play_pause').click(function(){
			$(this).toggleClass('playing');
		});
		
		if ($("#map_canvas").length){
			initGoogleMap();
		}
		
	});


	jQuery(document).ready(function($) {
	
		/*  Toggle header search
		/* ------------------------------------ */
		$('.toggle-search').click(function(){
			$('.toggle-search').toggleClass('active');
			$('.search-expand').fadeToggle(250);
	            setTimeout(function(){
	                $('.search-expand input').focus();
	            }, 300);
		});
		
		/*  Scroll to top
		/* ------------------------------------ */
		$('a#back-to-top').click(function() {
			$('html, body').animate({scrollTop:0},'slow');
			return false;
		});
		
		/*  Tabs widget
		/* ------------------------------------ */	
		(function() {
			var $tabsNav       = $('.alx-tabs-nav'),
				$tabsNavLis    = $tabsNav.children('li'),
				$tabsContainer = $('.alx-tabs-container');
	
			$tabsNav.each(function() {
				var $this = $(this);
				$this.next().children('.alx-tab').stop(true,true).hide()
				.siblings( $this.find('a').attr('href') ).show();
				$this.children('li').first().addClass('active').stop(true,true).show();
			});
	
			$tabsNavLis.on('click', function(e) {
				var $this = $(this);
	
				$this.siblings().removeClass('active').end()
				.addClass('active');
				
				$this.parent().next().children('.alx-tab').stop(true,true).hide()
				.siblings( $this.find('a').attr('href') ).fadeIn();
				e.preventDefault();
			}).children( window.location.hash ? 'a[href=' + window.location.hash + ']' : 'a:first' ).trigger('click');
	
		})();
		
		/*  Comments / pingbacks tabs
		/* ------------------------------------ */	
	    $(".comment-tabs li").click(function() {
	        $(".comment-tabs li").removeClass('active');
	        $(this).addClass("active");
	        $(".comment-tab").hide();
	        var selected_tab = $(this).find("a").attr("href");
	        $(selected_tab).fadeIn();
	        return false;
	    });
	
		/*  Table odd row class
		/* ------------------------------------ */
		$('table tr:odd').addClass('alt');
	
		/*  Sidebar collapse
		/* ------------------------------------ */
		$('body').addClass('s1-collapse');
		$('body').addClass('s2-collapse');
		
		$('.s1 .sidebar-toggle').click(function(){
			$('body').toggleClass('s1-collapse').toggleClass('s1-expand');
			if ($('body').is('.s2-expand')) { 
				$('body').toggleClass('s2-expand').toggleClass('s2-collapse');
			}
		});
		$('.s2 .sidebar-toggle').click(function(){
			$('body').toggleClass('s2-collapse').toggleClass('s2-expand');
			if ($('body').is('.s1-expand')) { 
				$('body').toggleClass('s1-expand').toggleClass('s1-collapse');
			}
		});
	
		/*  Dropdown menu animation
		/* ------------------------------------ */
		$('.nav ul.sub-menu').hide();
		$('.nav li').hover( 
			function() {
				$(this).children('ul.sub-menu').slideDown('fast');
			}, 
			function() {
				$(this).children('ul.sub-menu').hide();
			}
		);
		
		/*  Mobile menu smooth toggle height
		/* ------------------------------------ */	
		$('.nav-toggle').on('click', function() {
			slide($('.nav-wrap .nav', $(this).parent()));
		});
		 
		function slide(content) {
			var wrapper = content.parent();
			var contentHeight = content.outerHeight(true);
			var wrapperHeight = wrapper.height();
		 
			wrapper.toggleClass('expand');
			if (wrapper.hasClass('expand')) {
				setTimeout(function() {
					wrapper.addClass('transition').css('height', contentHeight);
				}, 10);
			} else {
				setTimeout(function() {
					wrapper.css('height', wrapperHeight);
					setTimeout(function() {
					wrapper.addClass('transition').css('height', 0);
					}, 10);
				}, 10);
			}

			wrapper.one('transitionEnd webkitTransitionEnd transitionend oTransitionEnd msTransitionEnd', function() {
				if(wrapper.hasClass('open')) {
					wrapper.removeClass('transition').css('height', 'auto');
				}
			});
		}
		
	});

})(jQuery);